<template>
	<el-container>
		<el-container>
			<el-main class="nopadding">
				<el-empty title="暂无数据" v-if="fileList.length==0"></el-empty>
				<sc-file-preview :fileItem="fileItem"></sc-file-preview>
			</el-main>
		</el-container>
		<el-aside>
			<el-container>
				<el-header style="text-align: center;font-size: 18px;font-weight: 600;display: unset;line-height: 30px">文档列表</el-header>
				<el-main>
					<el-empty title="暂无数据" v-if="fileList.length==0"></el-empty>
					<div v-for="item in fileList" class="fileItem">
						<div>{{item.name}}</div>
						<div style="margin: 5px;text-align: right">
							<span v-if="!config.PDF_FILE.includes(item.extName)&&!config.IMG_FILE.includes(item.extName)">
								<el-button type="text" v-if="publicFileRole=='4'" @click="editFile(item.attachId,item.extName)">编辑</el-button>
								<div v-if="publicFileRole=='5'" style="display: inline">
									<el-button type="text" v-if="config.DOC_FILE.includes(item.extName)" @click="taoHongFile(item.attachId,item.extName)">套红</el-button>
									<el-button type="text" v-else @click="editFile(item.attachId,item.extName)">编辑</el-button>
								</div>
							</span>
							<el-button type="text" @click="showFile(item)" style="margin-left: 10px">查看</el-button>
						</div>
					</div>
				</el-main>
			</el-container>
		</el-aside>
	</el-container>
</template>

<script>
import config from "@/config";
import sysConfig from "@/config";
export default {
	computed: {
		config() {
			return config
		}
	},
	name: 'Doc File Preview',
	props:{
		attachIds:{type:String},
		publicFileRole:{type:String},
	},
	components: {

	},
	data() {
		return {
			extName:'',
			attachId:'',
			fileItem:'',
			fileList:[]
		}
	},
	watch:{
		attachIds(){
			if(this.attachIds!='')
			{
				this.getAttachList();
			}
		}
	},
	mounted() {
	},
	methods:{
		async getAttachList() {
			if (this.attachIds !== '' && this.attachIds !== undefined) {
				let res = await this.$API.file.attach.getAttachList.get({attachIds: this.attachIds});
				if (res.code === 200) {
					this.fileList = res.data;
				} else {
					this.$alert(res.message, "提示", {type: 'error'})
				}
				if(this.fileList.length>0)
				{
					this.showFile(this.fileList[0])
				}
			}
		},
		showFile(fileItem)
		{
			this.fileItem = fileItem;
		},
		editFile(attachId, extName) {
			if (this.$TOOL.data.get("OFFICE_TYPE") === '0') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openword?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openexcel?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openppt?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					if (this.$TOOL.data.get("OFD_TYPE") !== '0') {
						this.openPdfFile(attachId, extName, 4);
					} else {
						POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openpdf?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
					}
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			} else if (this.$TOOL.data.get("OFFICE_TYPE") === '2') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openword?attachId=' + attachId + '&extName=' + extName + '&openModeType=4&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openexcel?attachId=' + attachId + '&extName=' + extName + '&openModeType=4&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openppt?attachId=' + attachId + '&extName=' + extName + '&openModeType=4&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					this.openPdfFile(attachId, extName, 4)
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName;
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}else if (this.$TOOL.data.get("OFFICE_TYPE") === '3') {
				if(sysConfig.DOC_FILE.includes(extName)||sysConfig.EXCEL_FILE.includes(extName)||sysConfig.PPT_FILE.includes(extName))
				{
					window.open(this.$TOOL.data.get("ATTACH_HOST")+'/v3/weboffice/index?attachId='+attachId+'&extName='+extName+'&openModeType=4&token='+this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				}else if(sysConfig.PDF_FILE.includes(extName))
				{
					this.openPdfFile(attachId,extName,4)
				}else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL+'/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}
			this.$emit('success', this.uid);
		},
		taoHongFile(attachId, extName) {
			if (this.$TOOL.data.get("OFFICE_TYPE") === '0') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/taohongword?stepRunId=' + this.stepRunId + '&attachId=' + attachId + '&extName=' + extName + '&openModeType=5', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openexcel?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openppt?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					if (this.$TOOL.data.get("OFD_TYPE") !== '0') {
						this.openPdfFile(attachId, extName, 4);
					} else {
						POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openpdf?attachId=' + attachId + '&extName=' + extName + '&openModeType=4', 'width=1200px;height=800px;scroll=no;');
					}
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			} else if (this.$TOOL.data.get("OFFICE_TYPE") === '2') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openword?attachId=' + attachId + '&extName=' + extName + '&openModeType=5&model=1&stepRunId=' + this.stepRunId + '&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openexcel?attachId=' + attachId + '&extName=' + extName + '&openModeType=5&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openppt?attachId=' + attachId + '&extName=' + extName + '&openModeType=5&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					this.openPdfFile(attachId, extName, 5)
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}else if (this.$TOOL.data.get("OFFICE_TYPE") === '3') {
				if(sysConfig.DOC_FILE.includes(extName)||sysConfig.EXCEL_FILE.includes(extName)||sysConfig.PPT_FILE.includes(extName))
				{
					window.open(this.$TOOL.data.get("ATTACH_HOST")+'/v3/weboffice/index?attachId='+attachId+'&extName='+extName+'&openModeType=5&token='+this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				}else if(sysConfig.PDF_FILE.includes(extName))
				{
					this.openPdfFile(attachId,extName,5)
				}else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL+'/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}
			this.$emit('success', this.uid);
		},
		downFile(attachId, extName) {
			if (this.isPc) {
				window.open(sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true", "_blank");
			} else {
				this.$API.file.attach.downLoadFile.fileDownload({attachId: attachId, extName: extName});
			}
			this.$emit('success', this.uid);
		},
		printFile(attachId, extName) {
			if (this.$TOOL.data.get("OFFICE_TYPE") === '0') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openword?attachId=' + attachId + '&extName=' + extName + '&openModeType=3', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openexcel?attachId=' + attachId + '&extName=' + extName + '&openModeType=3', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openppt?attachId=' + attachId + '&extName=' + extName + '&openModeType=3', 'width=1200px;height=800px;scroll=no;');
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					if (this.$TOOL.data.get("OFD_TYPE") !== '0') {
						this.openPdfFile(attachId, extName, 3);
					} else {
						POBrowser.openWindowModeless(this.$TOOL.data.get("ATTACH_HOST") + '/office/openpdf?attachId=' + attachId + '&extName=' + extName + '&openModeType=3', 'width=1200px;height=800px;scroll=no;');
					}
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			} else if (this.$TOOL.data.get("OFFICE_TYPE") === '2') {
				if (sysConfig.DOC_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openword?attachId=' + attachId + '&extName=' + extName + '&openModeType=3&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.EXCEL_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openexcel?attachId=' + attachId + '&extName=' + extName + '&openModeType=3&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.PPT_FILE.includes(extName)) {
					window.open(this.$TOOL.data.get("ATTACH_HOST") + '/office/wps/openppt?attachId=' + attachId + '&extName=' + extName + '&openModeType=3&token=' + this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				} else if (sysConfig.PDF_FILE.includes(extName)) {
					this.openPdfFile(attachId, extName, 3)
				} else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL + '/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName+"&ddtab=true";
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}else if (this.$TOOL.data.get("OFFICE_TYPE") === '3') {
				if(sysConfig.DOC_FILE.includes(extName)||sysConfig.EXCEL_FILE.includes(extName)||sysConfig.PPT_FILE.includes(extName))
				{
					window.open(this.$TOOL.data.get("ATTACH_HOST")+'/v3/weboffice/index?attachId='+attachId+'&extName='+extName+'&openModeType=3&token='+this.$TOOL.cookie.get("TOKEN")+"&ddtab=true");
				}else if(sysConfig.PDF_FILE.includes(extName))
				{
					this.openPdfFile(attachId,extName,3)
				}else if (sysConfig.IMG_FILE.includes(extName)) {
					const image = new Image()
					image.src = sysConfig.API_URL+'/get/file/getFileDown?attachId=' + attachId + "&extName=" + extName;
					image.onload = () => {
						image.style.margin = "0 auto"
						image.style.display = "block"
						const newWin = window.open("", "_blank")
						newWin.document.write(image.outerHTML)
					}
				}
			}
			this.$emit('success', this.uid);
		},
	}
}
</script>

<style scoped>
.fileItem{
	margin: 5px 10px 5px 10px;
	font-size: 14px;
	border-bottom: 1px solid var(--el-border-color-light);
}
</style>
